package com.mingye.listener;

import com.mingye.components.BoardPanel;
import com.mingye.components.GameFrame;
import com.mingye.constants.Direction;
import com.mingye.model.chess.Person;

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

/**
 * 人物移动的键盘事件
 *
 * @author daixinyi
 */
public class MoveKeyListener extends KeyAdapter {

    private GameFrame frame;

    public MoveKeyListener(GameFrame frame) {
        this.frame = frame;
    }

    /**
     * 键盘按下事件
     *
     * @param e
     */
    @Override
    public void keyPressed(KeyEvent e) {
        if (frame.isVictoryFlag()) {
            return;
        }
        int keyCode = e.getKeyCode();
        if (keyCode == KeyEvent.VK_UP || keyCode == KeyEvent.VK_DOWN || keyCode == KeyEvent.VK_LEFT || keyCode == KeyEvent.VK_RIGHT ||
                keyCode == KeyEvent.VK_W || keyCode == KeyEvent.VK_S || keyCode == KeyEvent.VK_A || keyCode == KeyEvent.VK_D) {
            BoardPanel boardPanel = frame.getBoardPanel();
            Person person = boardPanel.getBoard().getPerson();
            switch (keyCode) {
                case KeyEvent.VK_UP:
                case KeyEvent.VK_W:
                    person.move(Direction.UP);
                    break;
                case KeyEvent.VK_DOWN:
                case KeyEvent.VK_S:
                    person.move(Direction.DOWN);
                    break;
                case KeyEvent.VK_LEFT:
                case KeyEvent.VK_A:
                    person.move(Direction.LEFT);
                    break;
                case KeyEvent.VK_RIGHT:
                case KeyEvent.VK_D:
                    person.move(Direction.RIGHT);
                    break;
            }
            boardPanel.repaint();
        }
    }
}
